use "C:\Country Database"
gen mo=month+(year-2005)*12
replace mo=mo-12
gen q=1
replace q=2 if month==4 | month==5 | month==6
replace q=3 if month==7 | month==8 | month==9
replace q=4 if month==10 | month==11 | month==12

sort country mo
save "C:\z1",replace
egen tc=tag(country)
keep if tc==1
gen cno=_n
keep country cno
sort country
save "C:\z2",replace
use "C:\z1"
merge country using "C:\z2"
drop _merge
sort cno mo
tsset cno mo

mvsumm pd,stat(mean) win(24) gen(cpd) end
save "C:\w1",replace
replace cpd=12*cpd
drop if cpd==.
collapse (mean) cpd, by(country year q)
by country: gen cpd1=cpd[1]
by country: gen cpdf=cpd[13]
sort country year q
save "C:\x0",replace
egen tc=tag(country)
keep if tc==1
gen mark=1
keep country cpdf mark
sort mark
save "C:\x1",replace
joinby mark using "C:\Add Quarters" 
gen qq=q+(year-2011)*4

** Set year when cpd reaches 0

gen cyear=2020
gen ct=cpdf/((1+cyear-2011)*4)
gen cpd=cpdf-ct*qq
replace cpd=0 if year==cyear & q==4
replace cpd=0 if year>cyear  
keep country year q cpd
sort country year q
save "C:\x2",replace
use "C:\x0"
append using "C:\x2",
sort country year q
merge country using "C:\SE Asia Country Multipliers"
drop _merge
sort country year q
merge country using "C:\Transition Years"
drop _merge
drop cpd1 cpdf
sort country year q
by country: gen dcpd=cpd-cpd[_n-1]
gen zdcpd=dcpd
by country: gen cpd1=mav*cpd[1]
by country: gen cpdb=cpd[1]
gen qq=q+(year-2008)*4

** Set year when REDD line reaches 0

gen ryear=2025
gen rtrack=cpd1/((1+ryear-2008)*4)
gen rline=cpd1-rtrack*qq
replace rline=0 if year==ryear & q==4
replace rline=0 if year>ryear  
gen xrline=rline
gen lcpd=cpd-rline
gen zlcpd=lcpd
gen qlx=qq
replace qlx=. if lcpd>0
replace qlx=. if year==2007
bysort country: egen mqlx=min(qlx)
replace lcpd=0 if qq<mqlx
bysort country: gen rstart=xrline[mqlx]
gen rdiff=rstart-xrline
replace rdiff=0 if qq<mqlx
bysort country: gen srdiff=sum(rdiff)

** Set year when transition line reaches 0

gen tyear=date30
gen ttrack=cpd1/((1+tyear-2008)*4)
gen tline=cpd1-ttrack*qq
replace tline=0 if year==tyear & q==4
replace tline=0 if year>tyear  
gen xtline=tline
gen gcpd=cpd-tline
gen zgcpd=gcpd
gen qgx=qq
replace qgx=. if gcpd>0
replace qgx=. if year==2007
bysort country: egen mqgx=min(qgx)
replace gcpd=0 if qq<mqgx
bysort country: gen tstart=xtline[mqlx]
gen tdiff=tstart-xtline
replace tdiff=0 if qq<mqgx
bysort country: gen stdiff=sum(tdiff)

sort country year q
merge country year q using "C:\Quarterly Adjusted"
drop _merge
replace score=999 if year>2010
gen ydcpd=dcpd
replace dcpd=0 if dcpd<0 & score==0
gen r=""
replace r="Green" if cpd==0 | (zgcpd<0 & zlcpd<0 & dcpd<0)
replace r="Aqua" if zgcpd<0 & zlcpd<0 & dcpd>=0 & cpd>0
replace r="Blue" if zgcpd<=0 & zlcpd>=0 & dcpd<0
replace r="Yellow" if zgcpd<=0 & zlcpd>=0 & dcpd>=0 & cpd>0
replace r="Orange" if zgcpd>0 & zlcpd>0 & dcpd<0
replace r="Red" if zgcpd>0 & zlcpd>0 & dcpd>=0
gen s=0
replace s=1 if r=="Red"
replace s=2 if r=="Orange"
replace s=3 if r=="Yellow"
replace s=4 if r=="Blue"
replace s=5 if r=="Aqua"
replace s=6 if r=="Green"
save "C:\w0",replace
* drop if year<2008
sort country year q
save "C:\z3",replace
sort country year q
gen xgcpd=-gcpd
gen xlcpd=-lcpd
replace dcpd=-dcpd
by country: gen sgcpd=sum(xgcpd)
by country: gen slcpd=sum(xlcpd)
replace dcpd=0 if dcpd<0
replace sgcpd=0 if sgcpd<0
replace slcpd=0 if slcpd<0

gen tcpd=gcpd+lcpd+dcpd

gen nlcpd=slcpd-srdiff
replace nlcpd=0 if nlcpd<0

gen ngcpd=sgcpd-stdiff
replace ngcpd=0 if ngcpd<0
 
sort country year q
save "C:\xx1",replace


* Payment calculus: Set CO2 price at $50/ton.  Pay $5/tCO2 rent/year (10% interest).  One hectare * cleared yields * 500 tons CO2.  So one sq. km. yields 50,000 tons CO2.  
* So each sq. km. not cleared yields 50,000 x $5.00  

gen co2pr=40
gen irate=.0545
gen qrate=(1+irate)^(1/4)-1
gen reddm=1
gen diffm=100

** Transition payment per sq. km. is co2 price *interest rate * 500 tons/hectare * 100 hectares/sq.km.

gen pgcpd=co2pr*qrate*500*100
gen plcpd=reddm*pgcpd
gen pdcpd=diffm*pgcpd

*gen pgcpd=250000
*gen plcpd=500000
*gen pdcpd=1000000

gen vgcpd=pgcpd*ngcpd/1000
gen vlcpd=plcpd*nlcpd/1000
gen vdcpd=pdcpd*dcpd/1000
replace vgcpd=0 if vgcpd==.
replace vlcpd=0 if vlcpd==.
replace vdcpd=0 if vdcpd==.
gen tvcpd=vgcpd+vlcpd+vdcpd
drop if year==.
gen syear=string(year)
gen sq=string(q)
gen syq=syear+" q"+sq

** Allocation between government and indigenous peoples

gen govshr=.8
gen indshr=.2

gen govpay=govshr*tvcpd
gen indpay=indshr*tvcpd
replace vgcpd= vgcpd/1000 
replace vlcpd= vlcpd/1000 
replace vdcpd= vdcpd/1000 
replace tvcpd= tvcpd/1000 
replace govpay= govpay/1000 
replace indpay= indpay/1000 
format vgcpd vlcpd vdcpd tvcpd govpay indpay %10.1f
gen mdate=yq(year,q)
format mdate %tq
sort country syq
save "C:\Country Change Indicators",replace
outsheet country syq vgcpd vlcpd vdcpd tvcpd govpay indpay using "C:\Country COD by Quarter",replace
collapse (sum) vgcpd vlcpd vdcpd tvcpd govpay indpay, by(country year)
outsheet country year vgcpd vlcpd vdcpd tvcpd govpay indpay using "C:\Country COD by Year",replace
save "C:\z6",replace
collapse (sum) vgcpd vlcpd vdcpd tvcpd govpay indpay , by(year)
outsheet year vgcpd vlcpd vdcpd tvcpd govpay indpay using "C:\COD by Year",replace

clear

use "C:\z6"
collapse (sum) vgcpd vlcpd vdcpd tvcpd govpay indpay, by(country)
outsheet country vgcpd vlcpd vdcpd tvcpd govpay indpay using "C:\COD by Country",replace

clear

use "C:\Country Change Indicators" 
format ngcpd nlcpd dcpd tcpd %8.2f
outsheet country syq ngcpd nlcpd dcpd tcpd using "C:\Country Points by Quarter",replace
collapse (sum) ngcpd nlcpd dcpd tcpd, by(country year)
outsheet country year ngcpd nlcpd dcpd tcpd using "C:\Country Points by Year",replace
save "C:\z7",replace
collapse (sum) ngcpd nlcpd dcpd tcpd, by(year)
outsheet year ngcpd nlcpd dcpd tcpd using "C:\Points by Year",replace

clear
use "C:\z7"
collapse (sum) ngcpd nlcpd dcpd tcpd, by(country)
outsheet country ngcpd nlcpd dcpd tcpd using "C:\Points by Country",replace

clear

use "C:\Country Change Indicators"
gen gc=ngcpd
gen lc=nlcpd
gen dc=dcpd
keep country syq gc lc dc
sort country syq
save "C:\z4",replace

use "C:\z3"
gen syear=string(year)
gen sq=string(q)
gen syq=syear+" q"+sq
gen g=gcpd
gen l=lcpd
gen d=dcpd
gen c=cpd
gen c1=cpd1
gen rl=rline
keep country qq syq s r c c1 rl g l d
sort country syq
merge country syq using "C:\z4"
drop _merge
sort country syq
save "C:\z5",replace
drop s
reshape wide gc lc dc c c1 rl r g l d, i(syq) j(country) string
format gc* lc* dc* c* c1* rl* g* l* d* %7.3f
save "C:\ratings by color",replace
outsheet using "C:\full country scores",replace

use "C:\z5"
keep gc lc dc r syq country
reshape wide gc lc dc r, i(syq) j(country) string
format gc* lc* dc* %7.3f
save "C:\scores and ratings",replace
outsheet syq r* using "C:\ratings",replace

use "C:\Country Change Indicators"
collapse (sum) ngcpd nlcpd dcpd, by(country)
gen tcpd=ngcpd+nlcpd+dcpd
gen pgcpd=100*ngcpd/tcpd
gen plcpd=100*nlcpd/tcpd
gen pdcpd=100*dcpd/tcpd
replace pgcpd=0 if pdcpd==.
replace plcpd=0 if plcpd==.
replace pdcpd=0 if pdcpd==.
egen tgcpd=total(ngcpd)
egen tlcpd=total(nlcpd)
egen tdcpd=total(dcpd)
egen ttcpd=total(tcpd)
gen cgcpd=100*ngcpd/tgcpd
gen clcpd=100*nlcpd/tlcpd
gen cdcpd=100*dcpd/tdcpd
gen ctcpd=100*tcpd/ttcpd
format ngcpd nlcpd dcpd tcpd pgcpd plcpd pdcpd cgcpd clcpd cdcpd ctcpd %8.1f
outsheet country pgcpd plcpd pdcpd cgcpd clcpd cdcpd ctcpd using "C:\Country Percents",replace

clear

*** Scores for Color-Coded Figure 6

use "C:\z5"
sort country syq 
format g l d c c1 rl %8.2f 
outsheet country syq r g l d c c1 rl using "C:\Country Ratings With Data",replace
keep country syq s
reshape wide s, i(syq) j(country) string
gen year=substr(syq,1,4)
gen q=substr(syq,-1,1)
destring year, replace
destring q, replace
gen qq=q+(year-2008)*4
outsheet syq s* using "C:\Country Ratings Alone",replace

